package com.s.server;

import android.text.TextUtils;

import io.grpc.stub.StreamObserver;
import s.log.Code;
import s.log.Level;
import s.log.Message;
import s.log.RPCLogGrpc;
import s.log.Response;

public class LogServerImpl extends RPCLogGrpc.RPCLogImplBase {

    @Override
    public void report(Message request, StreamObserver<Response> reply) {
        try {
            logInternal(request);
        } finally {
            reply.onNext(Response.newBuilder().setCode(Code.EN__EN___VALUE).build());
            reply.onCompleted();
        }
    }

    private void logInternal(Message msg) {
        String tag = TextUtils.isEmpty(msg.getTag()) ? "" : msg.getTag();
        switch (msg.getLevel()) {
            case Level.I_VALUE: {
                LocalLog.i(tag, msg.getMsg());
                break;
            }
            case Level.D_VALUE: {
                LocalLog.d(tag, msg.getMsg());
                break;
            }
            case Level.W_VALUE: {
                LocalLog.w(tag, msg.getMsg());
                break;
            }
            case Level.E_VALUE: {
                LocalLog.e(tag, msg.getMsg());
                break;
            }
        }
    }

}
